package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type AddressInfoService struct {
}

// CreateAddressInfo 创建AddressInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (addressInfoService *AddressInfoService) CreateAddressInfo(addressInfo autocode.AddressInfo) (err error) {
	err = global.GVA_DB.Create(&addressInfo).Error
	return err
}

// DeleteAddressInfo 删除AddressInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (addressInfoService *AddressInfoService) DeleteAddressInfo(addressInfo autocode.AddressInfo) (err error) {
	err = global.GVA_DB.Delete(&addressInfo).Error
	return err
}

// DeleteAddressInfoByIds 批量删除AddressInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (addressInfoService *AddressInfoService) DeleteAddressInfoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.AddressInfo{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateAddressInfo 更新AddressInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (addressInfoService *AddressInfoService) UpdateAddressInfo(addressInfo autocode.AddressInfo) (err error) {
	err = global.GVA_DB.Save(&addressInfo).Error
	return err
}

// GetAddressInfo 根据id获取AddressInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (addressInfoService *AddressInfoService) GetAddressInfo(id uint) (err error, addressInfo autocode.AddressInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&addressInfo).Error
	return
}

// GetAddressInfoInfoList 分页获取AddressInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (addressInfoService *AddressInfoService) GetAddressInfoInfoList(info autoCodeReq.AddressInfoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.AddressInfo{}).Order("address_info.create_time desc")
	var addressInfos []autocode.AddressInfo
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.UserId != nil {
		db = db.Where("user_id = ?", info.UserId)
	}
	if info.GoodsId != nil {
		db = db.Where("goods_id = ?", info.GoodsId)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Joins("left join user_info on user_info.id = address_info.user_id").Joins("left join goods_info on goods_info.id = address_info.goods_id").Select(autocode.Selects).Find(&addressInfos).Error
	return err, addressInfos, total
}

// GetAddressInfoInfoListAll 分页获取AddressInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (addressInfoService *AddressInfoService) GetAddressInfoInfoListAll(info autoCodeReq.AddressInfoSearch) (err error, list []autocode.AddressInfo) {
	// 创建db
	db := global.GVA_DB.Model(&autocode.AddressInfo{}).Order("create_time desc")
	var addressInfos []autocode.AddressInfo
	err = db.Joins("left join user_info on user_info.id = address_info.user_id").Joins("left join goods_info on goods_info.id = address_info.goods_id").Select(autocode.Selects).Find(&addressInfos).Error
	return err, addressInfos
}
